package com.lvmama.pangolin.predict.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.core.AbstractService;
import com.lvmama.pangolin.entity.PredictCommodityListConditionVO;
import com.lvmama.pangolin.entity.predict.CommodityPricePredict;
import com.lvmama.pangolin.mybatis.mapper.predict.CommodityPricePredictMapper;
import com.lvmama.pangolin.predict.service.CommodityPricePredictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;


/**
 * Created by zhoutianqi on 2017/10/13.
 */
@Service
@Transactional
public class CommodityPricePredictServiceImpl extends AbstractService<CommodityPricePredict> implements CommodityPricePredictService {
    @Autowired
    private CommodityPricePredictMapper commodityPricePredictMapper;

    @Override
    public PageInfo<CommodityPricePredict> queryCommodityPredictList(PredictCommodityListConditionVO condition) {
        PageHelper.startPage(condition.getPage(), condition.getPageSize());
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("pre_date", DateUtil.getCurrentymd());
        List<CommodityPricePredict> resultList = null;
        //查询条件
        paramMap.put("company_code", condition.getCompanyCode());
        paramMap.put("product_name", condition.getProductName());
        // 默认查询驴妈妈产品 ，门票
        if (StringUtils.isEmpty(condition.getCompanyCode())) {
            paramMap.put("company_code", Constant.LMM_C_CODE);
        }

        if (!Constant.HOTEL_BUS_TYPE.equals(condition.getBusinessCode())) {
            String company_code = paramMap.get("company_code");
            String tableSuffix = company_code.substring(0, company_code.indexOf("_")).toLowerCase();
            paramMap.put("tableSuffix", tableSuffix);
            resultList = commodityPricePredictMapper.queryTicketCommodityPredictList(paramMap);
        } else {
            resultList = commodityPricePredictMapper.queryHotelCommodityPredictList(paramMap);
        }
        PageInfo<CommodityPricePredict> pageInfo = new PageInfo<CommodityPricePredict>(resultList);
        return pageInfo;
    }

    @Override
    public List<CommodityPricePredict> queryCommodityListById(String commodityId) {
        return commodityPricePredictMapper.queryCommodityListById(commodityId);
    }
}
